package vip.kassadin.user;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by kassadin@foxmail.com on 2017/5/4 0004.
 */

@RestController
@RequestMapping("/user")
public class UserController {

    private UserRepo userRepo;

    @Autowired
    public UserController(UserRepo userRepo) {
        this.userRepo = userRepo;
    }

    @GetMapping("/list")
    public List<User> users() {
        return userRepo.findAll();
    }

    @GetMapping("/{uid}")
    public User users(@PathVariable long uid) {
        return userRepo.findOne(uid);
    }

    @GetMapping("/add")
    public User addUser(@RequestParam String username, @RequestParam String password) {


        return userRepo.save(new User(username, password));
    }

    @GetMapping("/del")
    public String delUser(@RequestParam long id) {
        userRepo.delete(id);
        return "success";
    }

    @GetMapping("/{uid}/update")
    public User updateUser(@PathVariable long uid, @RequestParam String username) {
        User user = userRepo.findOne(uid);
        user.setUsername(username);
        return userRepo.save(user);
    }
}
